package com.ktwlrj.securitycenter.controller;

import com.ktwlrj.core.base.ActionResult;
import com.ktwlrj.core.base.DetailEntity;
import com.ktwlrj.core.base.SuperController;
import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.feign.FrameworkClient;
import com.ktwlrj.core.feign.McClient;
import com.ktwlrj.core.feign.args.SysCredentialsConfigArgs;
import com.ktwlrj.core.feign.args.ThirdCheckMemberSafeArgs;
import com.ktwlrj.core.feign.dto.*;
import com.ktwlrj.core.util.ContextHolderUtils;
import com.ktwlrj.securitycenter.service.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * 统一授权控制层
 *
 * @author zhouxuanhong
 * @date 2019-07-16 10:49
 * @since JDK1.8
 */
@Api(tags = "Sys", value = "Sys", description = "系统相关接口")
@Controller
@RequestMapping(value = "sys", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
@Validated
@RequiredArgsConstructor
public class SystemController extends SuperController {
    private final SystemService systemService;
    private final McClient mcClient;
    private final FrameworkClient frameworkClient;
    private final ContextHolderUtils contextHolderUtils;

    @ApiOperation(value = "获取用户基本信息", notes = "获取用户基本信息-v1.0")
    @GetMapping(value = "/select/user-info")
    @ResponseBody
    public ActionResult<DetailEntity> getUserInfo() {
        return success(contextHolderUtils.getDetailEntity());
    }

    @ApiOperation(value = "获取用户菜单权限", notes = "获取用户菜单权限-v1.0")
    @GetMapping("/select/menus")
    @ResponseBody
    public ActionResult<List<PermissionInfo>> getAuthMenu() {
        List<PermissionInfo> userMenu = new ArrayList<>();
        Object userAuthMenu = session.getAttribute("userAuthMenu");
        if (userAuthMenu != null) {
            userMenu = (List<PermissionInfo>) userAuthMenu;
        }
        return success(userMenu);
    }

    @GetMapping(value = "/accept/auth-info")
    @ApiOperation(value = "通过授权码进行登录授权", notes = "通过授权码进行登录授权-v1.0")
    @ResponseBody
    public ActionResult<List<PermissionInfo>> auth() {
        return success(systemService.auth(request));
    }

    @ApiOperation(value = "验证用户密码", notes = "验证用户密码-v1.0")
    @GetMapping(value = "/check/user-password")
    @ApiImplicitParam(paramType = "query", value = "用户密码", name = "password", required = true)
    @ResponseBody
    public ActionResult<Object> checkUserPassword(@NotBlank(message = "用户密码不能为空") String password) {
        // 验证用户密码
        DetailEntity detailEntity = contextHolderUtils.getDetailEntity();
        String account = StringUtils.isNotBlank(detailEntity.getMobilePhone())
                ? detailEntity.getMobilePhone()
                : detailEntity.getAccount();
        ThirdCheckMemberSafeArgs args = new ThirdCheckMemberSafeArgs();
        args.setPassword(password);
        args.setAccount(account);
        try {
            ActionResult<ThirdCheckSafeDto> checkMemberSafe = mcClient.getCheckMemberSafe(args);
            if (!checkMemberSafe.isSuccess()) {
                return error(ResultCode.VERIFICATION_FAILED.getValue(), "密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success("密码正确");
    }

    @GetMapping(value = "/select/regionTree")
    @ApiOperation(value = "获取行政区划下拉树", notes = "获取行政区划下拉树-v1.0")
    @ResponseBody
    public ActionResult<List<RegionTreeResult>> regionTree() {
        return frameworkClient.getRegionSelectTree();
    }

    @GetMapping(value = "/select/busiList")
    @ApiOperation(value = "获取数据权限业务列表", notes = "获取数据权限业务列表-v1.0")
    @ApiImplicitParam(name = "authCode", value = "权限编码", required = true, dataType = "String", paramType = "query")
    @ResponseBody
    public ActionResult<List<BusiSimpleInfo>> selectBusiList(@NotBlank(message = "权限编码不能为空") String authCode) {
        return success(systemService.getBusiList(authCode));
    }

    @ApiImplicitParam(name = "busiId", value = "业务配置id", required = true, dataType = "String", paramType = "query")
    @ApiOperation(value = "获取要件配置列表", notes = "获取要件配置列表接口")
    @GetMapping("/select/busiConfigList")
    @ResponseBody
    public ActionResult<List<SysCredentialsConfigArgs>> getBusiConfigList(@NotBlank(message = "业务配置id不能为空") String busiId) {
        return success(systemService.getBusiConfigOrCredentials(busiId));
    }

    @GetMapping(value = "/select/takeNumber")
    @ApiImplicitParam(paramType = "query", value = "取号编码", name = "tnRuleCode", required = true)
    @ApiOperation(value = "业务取号", notes = "业务取号-v1.0")
    @ResponseBody
    public ActionResult<String> selectTakeNumber(@NotBlank(message = "取号编码不能为空") String tnRuleCode) {
        return success(systemService.takeNumber(tnRuleCode));
    }


    @GetMapping(value = "/select/businessRule")
    @ApiImplicitParam(paramType = "query", value = "字典编码", name = "dictionaryCode", required = true)
    @ApiOperation(value = "业务规则列表", notes = "业务规则列表-v1.0")
    @ResponseBody
    public ActionResult<Object> selectBusinessRule(@NotBlank(message = "字典编码不能为空") String dictionaryCode) {
        return success(systemService.businessRule(dictionaryCode));
    }

    @GetMapping(value = "/select/memberListByOrgId")
    @ApiOperation(value = "获取用户所在机构下的所用人员列表", notes = "获取用户所在机构下的所用人员列表-v1.0")
    @ResponseBody
    public ActionResult<List<MemberDto>> selectMemberListByOrgId() {
        return success(systemService.getListMemberByOrgId());
    }
}
